package com.demo.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 在SpringBoot 里使用jsp ，要引入 org.apache.tomcat.embed/tomcat-embed-jasper 包
 * 还要做如下配置：
 *      spring.mvc.view.prefix=/WEB-INF/jsp/
 *      spring.mvc.view.suffix=.jsp
 *  注：
 *      1.webapp在main目录下和resource同级
 *      2.不能使用@RestController，要使用@Controller
 */
@Controller
public class LoginController {

    @PostMapping("/login")
    public String login(@RequestParam(value = "username", defaultValue = "admin") String usrName,
                        @RequestParam(value = "password", required = false, defaultValue = "123456") String pwd) {
        if ("admin".equals(usrName) && "123456".equals(pwd)) {
            return "ok";
        }
        return "fail";
    }

    /**
     * model.addAttribute 给jsp传参，类似于req.setAttribute
     * @param model
     * @return
     */
    @GetMapping("/login")
    public String login(Model model) {
        model.addAttribute("username","cxl");
        return "login";
    }
}
